<template>
  <div id="app">
    <div id="app" style="width: 400px">
      <el-form ref="param" :model="param" label-width="120px" class="el-form-css" status-icon>
        <el-form-item label="当前密码:" prop="oldPassword">
          <el-input type="password" v-model="param.oldPassword" show-password></el-input>
        </el-form-item>
        <el-form-item label="新密码:" prop="newPassword">
          <el-input type="password" v-model="param.newPassword" placeholder="由英文和数字组成且6-16位" show-password></el-input>
        </el-form-item>
        <el-form-item label="确认密码:" prop="newPassword2">
          <el-input type="password" v-model="param.newPassword2" placeholder="由英文和数字组成且6-16位" show-password></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onSubmit()">立即修改</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import {postPassword} from "@/api";

export default {
  name: "updatePassword",
  data() {
    return {
      param: {
        oldPassword: '',
        newPassword: '',
        newPassword2: '',
      }
    }
  }, methods: {
    onSubmit() {
      const pattern = /^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,16}$/
      if (this.param.oldPassword === null || this.param.oldPassword === '' || this.param.oldPassword === undefined) {
        this.$message.warning('请输入当前密码')
        throw "warning -> 请输入当前密码"
      }
      if (this.param.newPassword === null || this.param.newPassword === '' || this.param.newPassword === undefined) {
        this.$message.warning('请输入新密码')
        throw "warning -> 请输入新密码"
      }
      if (this.param.newPassword2 === null || this.param.newPassword2 === '' || this.param.newPassword2 === undefined) {
        this.$message.warning('请再次输入新密码')
        throw "warning -> 请再次输入新密码"
      }
      if (this.param.newPassword !== this.param.newPassword2)
        if (!this.param.newPassword2.match(pattern)) {
          this.$message.warning('新密码不一致')
          throw "warning -> 新密码不一致"
        }
      if (!this.param.newPassword2.match(pattern)) {
        this.$message.warning('密码格式错误')
        throw "warning -> 密码格式错误"
      }
      postPassword(this.param).then(res => {
        this.$message.success('修改成功！')
        return res
      })
    }
  }
}
</script>

<style scoped>

</style>